home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_include / LINUX / BYTEORDE.{9S / SWABB.H < prev   
C/C++ Source or Header  |  1999-09-17  |  3KB  |  124 lines

  1. #ifndef _LINUX_BYTEORDER_SWABB_H
  2. #define _LINUX_BYTEORDER_SWABB_H
  3.  
  4. /*
  5.  * linux/byteorder/swabb.h
  6.  * SWAp Bytes Bizarrely
  7.  *    swaHHXX[ps]?(foo)
  8.  *
  9.  * Support for obNUXIous pdp-endian and other bizarre architectures.
  10.  * Will Linux ever run on such ancient beasts? if not, this file
  11.  * will be but a programming pearl. Still, it's a reminder that we
  12.  * shouldn't be making too many assumptions when trying to be portable.
  13.  *
  14.  */
  15.  
  16. /*
  17.  * Meaning of the names I chose (vaxlinux people feel free to correct them):
  18.  * swahw32    swap 16-bit half-words in a 32-bit word
  19.  * swahb32    swap 8-bit halves of each 16-bit half-word in a 32-bit word
  20.  *
  21.  * No 64-bit support yet. I don't know NUXI conventions for long longs.
  22.  * I guarantee it will be a mess when it's there, though :->
  23.  * It will be even worse if there are conflicting 64-bit conventions.
  24.  * Hopefully, no one ever used 64-bit objects on NUXI machines.
  25.  *
  26.  */
  27.  
  28.  
  29. #define ___swahw32(x) \
  30.     ((__u32)( \
  31.         (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
  32.         (((__u32)(x) & (__u32)0xffff0000UL) >> 16) ))
  33. #define ___swahb32(x) \
  34.     ((__u32)( \
  35.         (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \
  36.         (((__u32)(x) & (__u32)0xff00ff00UL) >> 8) ))
  37.  
  38. /*
  39.  * provide defaults when no architecture-specific optimization is detected
  40.  */
  41. #ifndef __arch__swahw32
  42. #  define __arch__swahw32(x) ___swahw32(x)
  43. #endif
  44. #ifndef __arch__swahb32
  45. #  define __arch__swahb32(x) ___swahb32(x)
  46. #endif
  47.  
  48. #ifndef __arch__swahw32p
  49. #  define __arch__swahw32p(x) __swahw32(*(x))
  50. #endif
  51. #ifndef __arch__swahb32p
  52. #  define __arch__swahb32p(x) __swahb32(*(x))
  53. #endif
  54.  
  55. #ifndef __arch__swahw32s
  56. #  define __arch__swahw32s(x) do { *(x) = __swahw32p((x)); } while (0)
  57. #endif
  58. #ifndef __arch__swahb32s
  59. #  define __arch__swahb32s(x) do { *(x) = __swahb32p((x)); } while (0)
  60. #endif
  61.  
  62.  
  63. /*
  64.  * Allow constant folding
  65.  */
  66. #if defined(__GNUC__) && (__GNUC__ >= 2) && defined(__OPTIMIZE__)
  67. #  define __swahw32(x) \
  68. (__builtin_constant_p((__u32)(x)) ? \
  69.  ___swahw32((x)) : \
  70.  __fswahw32((x)))
  71. #  define __swahb32(x) \
  72. (__builtin_constant_p((__u32)(x)) ? \
  73.  ___swahb32((x)) : \
  74.  __fswahb32((x)))
  75. #else
  76. #  define __swahw32(x) __fswahw32(x)
  77. #  define __swahb32(x) __fswahb32(x)
  78. #endif /* OPTIMIZE */
  79.  
  80.  
  81. extern __inline__ __const__ __u32 __fswahw32(__u32 x)
  82. {
  83.     return __arch__swahw32(x);
  84. }
  85. extern __inline__ __u32 __swahw32p(__u32 *x)
  86. {
  87.     return __arch__swahw32p(x);
  88. }
  89. extern __inline__ void __swahw32s(__u32 *addr)
  90. {
  91.     __arch__swahw32s(addr);
  92. }
  93.  
  94.  
  95. extern __inline__ __const__ __u32 __fswahb32(__u32 x)
  96. {
  97.     return __arch__swahb32(x);
  98. }
  99. extern __inline__ __u32 __swahb32p(__u32 *x)
  100. {
  101.     return __arch__swahb32p(x);
  102. }
  103. extern __inline__ void __swahb32s(__u32 *addr)
  104. {
  105.     __arch__swahb32s(addr);
  106. }
  107.  
  108. #ifdef __BYTEORDER_HAS_U64__
  109. /*
  110.  * Not supported yet
  111.  */
  112. #endif /* __BYTEORDER_HAS_U64__ */
  113.  
  114. #if defined(__KERNEL__)
  115. #define swahw32 __swahw32
  116. #define swahb32 __swahb32
  117. #define swahw32p __swahw32p
  118. #define swahb32p __swahb32p
  119. #define swahw32s __swahw32s
  120. #define swahb32s __swahb32s
  121. #endif
  122.  
  123. #endif /* _LINUX_BYTEORDER_SWABB_H */
  124.